import request from "@/utils/request";

import { processParams, processResult } from "@/utils/appUtil";

// export function productionManageUrl(url) {
//     return "http://npjwa.6o3.cn/" + url;
// }

// export function developApiUrl(url) {
//     return "http://npjwa.6o3.cn/" + url;
// }

var pro_manage_url = process.env.VUE_APP_BASE_API;
var pro_res_url = process.env.VUE_APP_BASE_RES_API;
// // pro_manage_url = "https://testmanage.ybcm1000.com/";
// var dev_manage_url = "http://a.f.com/";

// var pro_client_url = "https://apitbes.ybcm1000.com/";
// // pro_client_url= "https://testmanage.ybcm1000.com/";
// var dev_client_url = "http://a.f.com/";

/** 获取后台的接口 */
export function getManageUrl(url = "") {
  const apiUrl = process.env.VUE_APP_BASE_API; // 从环境变量中获取 API URL
  url = "manage/" + url;
  url = apiUrl + url;
  // console.log(url, "url");
  // var apiUrl = "";
  // if (process.env.NODE_ENV == "production") {
  //     //生产环境
  //     apiUrl = pro_manage_url + url;
  // } else {
  //     // 测试环境
  //     apiUrl = dev_manage_url + url;
  // }
  return url;
}

/** 获取客户端的接口 */
export function getClientUrl(url = "") {
  url = "client/" + url;
  // url =  url;
  var apiUrl = "";
  if (process.env.NODE_ENV == "production") {
    //生产环境
    apiUrl = pro_client_url + url;
  } else {
    // 测试环境
    apiUrl = dev_client_url + url;
  }
  return apiUrl;
}

export function getApiUrl(url = "") {
  url = getManageUrl("" + url);
  return url;
}

// export function getManageUrl(url = '') {
//     url = getManageUrl('manage/' + url)
//     return url;
// }

// 获取资源路径
export function getResUrl(url = "") {
  url = pro_res_url + url;
  // url = getManageUrl(url)
  return url;
}

/** 获取上传路径 */
export function getUploadingImgUrl() {
  url = pro_res_url + url;
  // url = getManageUrl(url)
  return url;
}

/** POST请求 参数加工 结果数据加工 */
export function reqPost(url, data = {}) {
  return new Promise((resolve, reject) => {
    processParams({ ...data }).then((result_params) => {
      console.log(result_params, "result_params");
      post(url, result_params)
        .then((response) => {
          const result = response;
          if (result["code"] == 1) {
            processResult(result["data"], url).then((response) => {
              console.log(response, "response");
              result["data"] = response;
              resolve(result);
            });
          } else {
            reject(result);
          }
        })
        .catch(function (result) {
          reject(result);
        });
    });
  });
}

export function post(url, data = {}) {
  url = getManageUrl(url);
  return request({
    url: url,
    method: "post",
    data,
  });
}

export function apiPost(url, data = {}) {
  url = getClientUrl(url);
  return request({
    url: url,
    method: "post",
    data,
  });
}
